package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 订单管理
 * @author Fat
 */
@Api(description = "订单管理")
@RestController("AdminOrderController")
@RequestMapping("/admin/order")
public class OrderController {

    private static final Logger log = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;

    /**
     * 根据条件查询订单列表
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation(value = "根据条件查询订单列表")
    @GetMapping("/conditionSearch")
    public Result<Map<String,Object>> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("根据条件查询订单列表");
        Map<String,Object> map = orderService.queryByCondition(ordersPageQueryDTO);
        return Result.success(map);
    }

    /**
     * 统计订单数量
     * @return
     */
    @ApiOperation(value = "统计订单数量")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics(){
        log.info("统计订单数量");
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable("id") Long id){
        log.info("查询订单详情");
        OrderVO orderVO = orderService.queryById(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param orders
     * @return
     */
    @ApiOperation(value = "接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody Orders orders){
        log.info("确认订单");
        orders.setStatus(Orders.CONFIRMED);
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 拒单
     * @param orders
     * @return
     */
    @ApiOperation(value = "拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody Orders orders){
        log.info("拒单");
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        orders.setCancelReason(orders.getRejectionReason());
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 取消订单
     * @param orders
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody Orders orders){
        log.info("取消订单");
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @ApiOperation(value = "派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable("id") Long id){
        log.info("配送订单");
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderService.confirm(orders);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @ApiOperation(value = "完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable("id") Long id){
        log.info("配送订单");
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());
        orderService.confirm(orders);
        return Result.success();
    }

}
